package com.huike.web.controller.system;

import com.huike.clues.domain.SysMenu;
import com.huike.clues.domain.dto.my.DictExportDTO;
import com.huike.clues.domain.dto.my.DictQueryDTO;
import com.huike.clues.domain.dto.my.MenuQueryDTO;
import com.huike.clues.service.ISysMenuService;
import com.huike.common.annotation.Log;
import com.huike.common.annotation.RepeatSubmit;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysMenuDTO;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单信息
 */
@Slf4j
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {

    @Autowired
    private ISysMenuService sysMenuService;
    /**
     * 新增菜单数据
     *
     * @param sysMenuDTO
     * @return
     */
    @Log(title = "菜单管理", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public AjaxResult addDictData(@RequestBody SysMenuDTO sysMenuDTO) {
        log.info("新增菜单数据:{}", sysMenuDTO);
        return toAjax(sysMenuService.addSysMenu(sysMenuDTO));
    }


    /**
     * 修改菜单
     */
    @PutMapping
    public AjaxResult updateSysMenu(@RequestBody SysMenuDTO sysMenuDTO) {
        log.info("修改数据类型:{}", sysMenuDTO);
        return toAjax(sysMenuService.updateSysMenu(sysMenuDTO));
    }



    /**
     * 获取菜单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MenuQueryDTO menuQueryDTO) {
        startPage();
        List<SysMenu> list = sysMenuService.selectSysMenuList(menuQueryDTO);
        return getDataTable(list);
    }

    /**
     * 加载对应角色菜单列表树
     * @param roleId
     * @return
     */
    @GetMapping("/roleMenuTreeselect/{roleId}")
    public AjaxResult roleMenuTreeselect(@PathVariable Long roleId) {
        log.info("获取角色菜单树:{}", roleId);
        return AjaxResult.success(sysMenuService.selectMenuTreeByUserId(roleId));
    }

//    /**
//     * 获取菜单下拉树列表
//     * @return
//     */
//    @GetMapping("/treeselect")
//    public AjaxResult treeselect() {
//        return AjaxResult.success(sysMenuService.selectMenuTree());
//    }

    /**
     * 删除菜单.存在子菜单或已分配,不允许删除
     *
     * @param menuId
     * @return
     */
    @DeleteMapping("/{menuId}")
    public AjaxResult delectSysMenu(@PathVariable Long menuId) {
        return toAjax(sysMenuService.delectSysMenu(menuId));
    }

    /**
     * 根据菜单编号获取详细信息
     *
     * @return
     */
    @GetMapping("/{menuId}")
    public AjaxResult selectSysMenu(@PathVariable Long menuId) {
        SysMenu sysMenu = sysMenuService.selectSysMenu(menuId);
        return AjaxResult.success(sysMenu);
    }
}
